﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business.Partner;
using PJ.Business.Inventory;
using PJ.Data.SalePurchase;

namespace PJ.Business.SalePurchase
{
    /// <summary>
    /// Invoice item class
    /// </summary>
    /// <typeparam name="OD">Inner data object</typeparam>
    public abstract class InvoiceItem<OD> : OrderedFragment<OD>, IInvoiceItem
        where OD : IInvoiceItemData
    {
        private IInventoryItemService m_InventoryItemService;
        private IInventoryItem m_InventoryItem;

        public InvoiceItem()
        {
        }
        public InvoiceItem(OD data) : base (data)
        {
        }

        private IInventoryItemService InventoryItemService
        {
            get { return m_InventoryItemService; }
            set { m_InventoryItemService = value; }
        }

        public string PartnerID
        {
            get
            {
                return OwnerId;
            }
            set
            {
                if (value != OwnerId)
                {
                    OwnerId = value;
                }
            }
        }

        public string InventoryItemId
        {
            get
            {
                return this.ObjectData.InventoryItemId;
            }
            set
            {
                if (value != ObjectData.InventoryItemId)
                {
                    ObjectData.InventoryItemId = value;
                }
            }
        }

        public string Description
        {
            get
            {
                return ObjectData.Description;
            }
            set
            {
                if (ObjectData.Description != value)
                {
                    ObjectData.Description = value;
                }
            }
        }

        public double Quantity
        {
            get
            {
                return this.ObjectData.Quantity;
            }
            set
            {
                if (value != ObjectData.Quantity)
                {
                    ObjectData.Quantity = value;
                }
            }
        }
        public string UnitId
        {
            get
            {
                return ObjectData.UnitId;
            }
            set
            {
                if (ObjectData.UnitId != value)
                {
                    ObjectData.UnitId = value;
                }
            }
        }

        public double Price
        {
            get
            {
                return this.ObjectData.Price;
            }
            set
            {
                if (value != ObjectData.Price)
                {
                    ObjectData.Price = value;
                }
            }
        }
        public double Discount
        {
            get
            {
                return this.ObjectData.Discount;
            }
            set
            {
                if (value != ObjectData.Discount)
                {
                    ObjectData.Discount = value;
                }
            }
        }

        public double Total
        {
            get
            {
                return this.ObjectData.Total;
            }
            set
            {
                if (value != ObjectData.Total)
                {
                    ObjectData.Total = value;
                }
            }
        }

        public IInventoryItem InventoryItem
        {
            get
            {
                if (InventoryItemId != null && InventoryItemId != "")
                    if (m_InventoryItem == null)
                        m_InventoryItem = InventoryItemService.GetById(InventoryItemId, PJ.Data.GetDataMode.CachedFind) as IInventoryItem;
                return m_InventoryItem;
            }
            set
            {
                if (value != null)
                    InventoryItemId = value.Id;
                else
                    InventoryItemId = null;
            }
        }


        public override void CopyFrom(IBusinessObject data)
        {
            base.CopyFrom(data);
            IInvoiceItem idata = data as IInvoiceItem;
            if (idata != null)
            {
                this.InventoryItemId = idata.InventoryItemId; 
                this.Quantity = idata.Quantity;
                this.UnitId = idata.UnitId;
            }
        }

    }
}
